import { CancelToken } from "axios";
import { apiCacheWrapper } from "../../utils/cmm.util";

import { v3proRequest } from "../../utils/request.util";
import { V3proExternalDeviceTypeEnum } from "../../task/v3pro-task/model/v3pro.enum";
import {
  V3proExternalDeviceStatus,
  V3proPageResult,
  V3proScreen,
  V3proScreenAllStatus,
} from "./v3pro.api.response.model";

/**
 * v3pro 获取所有屏
 */
export const v3proScreenApi = apiCacheWrapper(
  (cancelToken: CancelToken, option = {}): Promise<V3proPageResult<V3proScreen[]>> => {
    return v3proRequest({
      url: "/screens",
      method: "GET",
      params: {
        limit: 1000,
      },
      cancelToken,
      ...option,
    });
  },
  (_) => "v3proScreenApi",
  "30m",
);

/**
 * v3pro 屏幕所有状态
 */
export const v3proScreenAllStatusApi = apiCacheWrapper(
  (screenId: string, cancelToken: CancelToken, option = {}): Promise<V3proScreenAllStatus> => {
    return v3proRequest({
      url: `/screens/${screenId}/allStatus`,
      method: "GET",
      cancelToken,
      ...option,
    });
  },
  (screenId) => screenId,
  "5s",
);

/**
 * 获取传感器列表
 */
export const v3proScreenExternalDeviceListApi = apiCacheWrapper(
  (
    screenId: string,
    cancelToken: CancelToken,
    option = {},
  ): Promise<{ id: string; name: string; type: V3proExternalDeviceTypeEnum }[]> => {
    return v3proRequest({
      url: `/screens/${screenId}/externalDevs`,
      method: "GET",
      cancelToken,
      ...option,
    });
  },
  (screenId) => screenId,
  "5s",
);

/**
 * 触发坏点检查
 * @param cancelToken
 * @param options
 */
export const v3proScreenStartDeadPixelCheck = (
  scanBoards: Array<{ id: string }>,
  cancelToken: CancelToken,
  options = {},
) => {
  return v3proRequest({
    url: `/status/badPixelCheck/start`,
    method: "POST",
    data: {
      destDevs: scanBoards,
    },
    cancelToken,
    ...options,
  });
};

/**
 * 获取传感器状态
 * @param deviceId 设备id
 * @param cancelToken
 * @param option
 */
export const v3proScreenExternalDeviceStatusApi = (
  deviceId: string,
  cancelToken: CancelToken,
  option = {},
): Promise<V3proExternalDeviceStatus> => {
  return v3proRequest({
    url: `/externalDevs/${deviceId}/status`,
    method: "GET",
    cancelToken,
    ...option,
  });
};
